package com.zjxf.controller;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.CmsSchoolTutor;
import com.zjxf.base.bean.ro.RoCmsSchoolTutor;
import com.zjxf.base.bean.vo.VoCmsSchoolTutor;
import com.zjxf.base.common.JSONResponse;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.service.CmsSchoolTutorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 长征大学导师
 *
 * @author ZhangWei
 * @date 2021/4/7/007 10:32
 */
@Validated
@RestController
@AllArgsConstructor
@Api(tags = "长征大学导师")
@RequestMapping("/cmsLmSchoolTutor/")
public class CmsLmSchoolTutorController {

    private final CmsSchoolTutorService cmsSchoolTutorService;

    @ApiOperation(value = "添加或修改")
    @PostMapping("addOrUpdate")
    public JSONResponse addOrUpdate(@Valid @RequestBody VoCmsSchoolTutor.addOrUpdate vo) {
        cmsSchoolTutorService.addOrUpdate(vo);
        return JSONResponse.success(SysConst.SUCCESS);
    }

    @ApiOperation(value = "删除")
    @PostMapping("delete")
    public JSONResponse delete(@NotNull(message = "请填写学校导师id") @RequestParam Long id) throws OperationException {
        cmsSchoolTutorService.delete(id);
        return JSONResponse.success(SysConst.SUCCESS);
    }

    @ApiOperation(value = "回显")
    @PostMapping("get")
    public JSONResponse get(@NotNull(message = "请填写学校导师id") @RequestParam Long id) throws OperationException {
        CmsSchoolTutor cmsSchoolTutor = cmsSchoolTutorService.getEntity(id);
        RoCmsSchoolTutor.Details roCmsLmSchoolTutor = cmsSchoolTutorService.getEntityRo(cmsSchoolTutor);
        return JSONResponse.success(roCmsLmSchoolTutor);
    }

    @ApiOperation(value = "分页")
    @PostMapping("page")
    public JSONResponse page(@Valid @RequestBody VoCmsSchoolTutor.filter filter) {
        Page<CmsSchoolTutor> cmsLmSchoolTutorPage = cmsSchoolTutorService.page(filter);
        JSONObject jsonData = cmsSchoolTutorService.pageRo(cmsLmSchoolTutorPage);
        return JSONResponse.success(jsonData);
    }
}
